package memory.net;

import java.awt.Dimension;
import java.net.Socket;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import memory.Player;
import memory.Tools;

@SuppressWarnings("serial")
public class DebugWindow extends JFrame{
	
	public static enum Run { Server, Client }
	
	private JTextArea chatArea = new JTextArea();
	private JScrollPane chatAreaPanel = new JScrollPane(chatArea);

	private String title = "T" + Tools.getRandomNr(99);
	
	public DebugWindow() {
		chatArea.setEditable(false);
		chatAreaPanel.setPreferredSize(new Dimension(400,250));
		
		add(chatAreaPanel);
		pack();
		
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	public DebugWindow(Run r) {
		//this.setSize(new Dimension(1000,650));
		chatArea.setEditable(false);
		chatAreaPanel.setPreferredSize(new Dimension(400,250));
		
		add(chatAreaPanel);
		pack();
		
		
		if(r == Run.Server) {
			startServing();
			setTitle("Server");
		}
		else {
			startConnecting();
			setTitle(title);
		}
		
		
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	public void startConnecting() {
		
		try {
			Socket socketen = new Socket("localhost",1337);
			socketen.setSoTimeout(50);
			Player playern = new Player(title);
			NetEventListener listenern = new NetEventListener();
			ClientThread threaden = new ClientThread(playern,socketen,listenern,this);
			listenern.setClientThread(threaden);
		}
		catch(Exception e) {}
		
	}
	
	public void startServing() {
		new ConnectClients("localhost",1337, this);
	}
	
	public void addMessage(String msg) {
		chatArea.append("> " + msg + "\n");
	}
	
}
